package code;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PhotoFile extends DateBasedFile implements IPhotoFile {

	/**
	 * Format the given date into a string with the given format.
	 * 
	 * @param format that the output is to be in
	 * @param date the date
	 * @return
	 */
	public static String formatDate(String format, Date date) {
		SimpleDateFormat outputFormatter = new SimpleDateFormat(format);
		return outputFormatter.format(date);
	}

	public PhotoFile(File file, IPhotoAlbum parent) {
		super(file, parent);

	}

	/*
	 * (non-Javadoc)
	 * @see code.IPhotoFile#move(code.PhotoAlbum)
	 */
	@Override
	public boolean move(IPhotoAlbum destination) {

		File newFile = new File(new File(destination.getFileName()), getNameOriginal());

		if (getFile().renameTo(newFile)) {
			setFile(newFile);
			setParent(destination);
			return true;
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see code.IPhotoFile#rename(java.lang.String)
	 */
	@Override
	public void rename(String alternateFormat) throws IOException {
		Date date = getDateFromName();
		if (null == date) {
			date = getParent().getDateFromName();
		}

		String formattedDate = formatDate("yyyyMMddDash", date); // TODO: Redo with DateFormat
		String extra = getExtra();
		String newFileName = String.format("%s - %s", formattedDate, extra);

		// so we have the newFileName now... but we should store the original somewhere for later.

		// TODO : Not done
		// make the change
		setNameNew(newFileName);
	}
}
